package com.qtcm.nas.cxm.bendianpinglun.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qtcm.nas.cxm.bendianpinglun.pojo.Comment_form;
import com.qtcm.nas.cxm.bendianpinglun.pojo.Orders;
import com.qtcm.nas.cxm.bendianpinglun.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class CommentController {
    @Autowired
    private ICommentService commentService;
//    @RequestMapping("/findAllCom")
//    public String getCom(Model model){
//        List<Orders> all = commentService.findAll();
//        model.addAttribute("all",all);
//        return "add_Comment";
//    }
    @RequestMapping("/findByIdCom")
    public String comments(Model model, @RequestParam(defaultValue = "0") int id){
        Orders byId = commentService.findById(id);
        model.addAttribute("byId",byId);
        List<Orders> all = commentService.findAll();
        model.addAttribute("all",all);
        return "add_Comment";
    }

    @RequestMapping("/addCom")
    public String addCom(Comment_form commentForm){
        commentForm.setD_order_time(new Date());
        commentService.addComment(commentForm);
        return "Comment";
    }
    @RequestMapping("findAllCom")
    public String findAllCom(Model model){
        List<Comment_form> allCom = commentService.findAllCom();
        model.addAttribute("allCom",allCom);
        return "Comment_findAll";
    }
    @RequestMapping("/findHp")
    public String fndHp(Model model){
        List<Comment_form> hp = commentService.findHp();
        //        model.addAttribute("hps",hps);
        model.addAttribute("allCom",hp);
        return "Comment_FindHp";
    }
    @RequestMapping("/findCp")
    public String findCp(Model model){
        List<Comment_form> cp = commentService.findCp();
        model.addAttribute("allCom",cp);
        return "Comment_FindCp";
    }
    @RequestMapping("findAllPageCom")
    public String findAllPage(Model model,HttpSession session,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "2") Integer pageSize){
        Long hps = commentService.countHp();
        Long cps = commentService.countCp();
        PageHelper.startPage(pageNum, pageSize);
        List<Comment_form> allPageCom = commentService.findAllPageCom(pageNum, pageSize);
        PageInfo<Comment_form>pageInfo = new PageInfo<>(allPageCom);
        model.addAttribute("pageInfo",pageInfo);
        session.setAttribute("hps",hps);
        session.setAttribute("cps",cps);
        return "Comment";
    }
    @RequestMapping("findOneCom")
    public String findOneCom(int cid,Model model){
        Comment_form oneCom = commentService.findOneCom(cid);
        model.addAttribute("oneCom",oneCom);
        return "view_Comment";
    }
}
